package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingPlanVo;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author xiaochang
 * @date 2025-07-22
 */
@Slf4j
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 查询计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        log.info("查询计划：{}",nursingPlan);

        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        log.info("查询计划Vo：{}",nursingPlanVo);
        // 查询计划项目计划关系
        List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        /**
         * 拷贝计划属性,设置计划的创建时间
         */
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());

        /**
         * 拷贝项目计划关系属性,设置计划项目计划关系表的创建时间
         */
        List<NursingProjectPlan> projectList = new ArrayList<>();
        projectList=nursingPlanDto.getProjectPlans();
        for ( NursingProjectPlan project : projectList){
            project.setCreateTime(DateUtils.getNowDate());
        }
        /**
         * 保存计划到数据库中
         */
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        /**
         * 批量保存护理项目计划关系到数据库
         */
        System.out.println("projectList:"+projectList + "nursingPlan.getId():"+nursingPlan.getId());
        int count =nursingProjectPlanMapper.insertNursingProjectPlanList(projectList,nursingPlan.getId());
        return count==0?0:1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        /**
         * 拷贝计划属性,设置计划的创建时间
         */
        NursingPlan nursingPlan = new NursingPlan();

        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        nursingPlan.setUpdateTime(DateUtils.getNowDate());

        /**
         * 拷贝项目计划关系属性,设置计划项目计划关系表的创建时间
         */
        List<NursingProjectPlan> projectList = new ArrayList<>();
        projectList=nursingPlanDto.getProjectPlans();

        nursingPlanMapper.updateNursingPlan(nursingPlan);

        log.info("projectList:"+projectList);
        int count=1;
        if ( nursingPlanDto.getProjectPlans()!=null  ){

            for ( NursingProjectPlan project : projectList){
                project.setCreateTime(DateUtils.getNowDate());
            }
            /**
             * 删除计划项目计划关系
             */
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            /**
             * 批量保存护理项目计划关系到数据库
             */
             count =nursingProjectPlanMapper.insertNursingProjectPlanList(projectList,nursingPlan.getId());
        }

        return count==0?0:1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        for (Long id : ids){
            nursingProjectPlanMapper.deleteByPlanId(id);
        }

        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        nursingProjectPlanMapper.deleteByPlanId(id);
        return nursingPlanMapper.deleteNursingPlanById(id);
    }
}
